package com.android.gpuimage;

import java.util.Locale;

/**
 * Camera 的美颜滤镜
 */

public class BeautyFilter extends GPUImageFilterGroup {

    private static float r8 = 1f; //???

    public BeautyFilter() {
        this(3);
    }

    public BeautyFilter(int level) {
        int floor = (int) Math.floor(Math.sqrt(Math.log(0.00390625d * Math.sqrt((Math.PI * 2.0d) * Math.pow((double) level, 2.0d))) * (-2.0d * Math.pow((double) level, 2.0d))));
        floor -= floor % 2;
        addFilter(new BeautySub1Filter(floor, level));
        addFilter(new BeautySub2Filter(floor, level));
    }

    public static String getVertexShader(int i, float f) {
        int i2;
        if (i < 1) {
            i = 1;
        }
        float[] fArr = new float[(i + 1)];
        float f2 = 0.0f;
        int i3 = 0;
        while (i3 < i + 1) {
            float f3;
            fArr[i3] = (float) ((1.0d / Math.sqrt((2.0d * Math.PI) * Math.pow((double) f, 2.0d))) * Math.exp((-Math.pow((double) i3, 2.0d)) / (2.0d * Math.pow((double) f, 2.0d))));
            if (i3 == 0) {
                f3 = fArr[i3] + f2;
            } else {
                f3 = (float) (((double) f2) + (2.0d * ((double) fArr[i3])));
            }
            i3++;
            f2 = f3;
        }
        for (i2 = 0; i2 < i + 1; i2++) {
            fArr[i2] = fArr[i2] / f2;
        }
        int min = Math.min((i / 2) + (i % 2), getMaxVaryingVectors());
        float[] fArr2 = new float[min];
        for (i2 = 0; i2 < min; i2++) {
            float f4 = fArr[(i2 * 2) + 1];
            float f5 = fArr[(i2 * 2) + 2];
            fArr2[i2] = ((f4 * ((float) ((i2 * 2) + 1))) + (f5 * ((float) ((i2 * 2) + 2)))) / (f4 + f5);
        }
        String str = (("uniform highp mat4 uTexMatrix; \n  attribute vec4 position;\n  attribute highp vec4 inputTextureCoordinate;\n  uniform highp float texelWidthOffset; \n  uniform highp float texelHeightOffset;\n  uniform highp float blurSize;\n  \n  varying highp vec2 textureCoordinate;\n"
                + String.format(Locale.US, "  varying highp vec2 blurCoordinates[%d];\n", new Object[]{Integer.valueOf((min * 2) + 1)}))
                + " void main() {\n  \tgl_Position = position;\n  \ttextureCoordinate =  (uTexMatrix * inputTextureCoordinate).xy;\n   highp vec2 singleStepOffset = vec2( texelWidthOffset,texelHeightOffset ) ;\n")
                + "blurCoordinates[0] = textureCoordinate;\n";
        for (i2 = 0; i2 < min; i2++) {
            str = (str + String.format(Locale.US, " blurCoordinates[%d] = textureCoordinate + singleStepOffset * %f;\n", new Object[]{Integer.valueOf((i2 * 2) + 1), Float.valueOf(fArr2[i2])}))
                    + String.format(Locale.US, " blurCoordinates[%d] = textureCoordinate - singleStepOffset * %f;\n", new Object[]{Integer.valueOf((i2 * 2) + 2), Float.valueOf(fArr2[i2])});
        }
        return str + "}  ";
    }

    public static int getMaxVaryingVectors() {
//        int[] values = new int[]{4};
//        GLES20.glGetIntegerv(GLES20.GL_MAX_VARYING_VECTORS, values, 0);
//        int i = (int) ((((double) values[0]) / 2.0d) - 1.0d);
//        if (i < 0) {
//            return 0;
//        }
//        return i;
        return Integer.MAX_VALUE;
    }

    public static String getFragmentShader1(int i, float f) {
        if (i < 1) {
            i = 1;
        }
        float[] fArr = new float[(i + 1)];
        float f2 = 0.0f;
        int i2 = 0;
        while (i2 < i + 1) {
            float f3;
            fArr[i2] = (float) ((1.0d / Math.sqrt((2.0d * Math.PI) * Math.pow((double) f, 2.0d))) * Math.exp((-Math.pow((double) i2, 2.0d)) / (2.0d * Math.pow((double) f, 2.0d))));
            if (i2 == 0) {
                f3 = fArr[i2] + f2;
            } else {
                f3 = (float) (((double) f2) + (2.0d * ((double) fArr[i2])));
            }
            i2++;
            f2 = f3;
        }
        for (int i3 = 0; i3 < i + 1; i3++) {
            fArr[i3] = fArr[i3] / f2;
        }
        i2 = Math.min((i / 2) + (i % 2), getMaxVaryingVectors());
        int i4 = (i / 2) + (i % 2);
        String str = ((((((((("varying highp vec2 textureCoordinate;\n \n  uniform highp float texelWidthOffset; \n  uniform highp float texelHeightOffset;\n" + String.format(Locale.US, " varying highp vec2 blurCoordinates[%d];\n", new Object[]{Integer.valueOf((i2 * 2) + 1)})) + "uniform int enableBlur;\n") + "uniform lowp float blurRadiusFromCenter;\n") + "uniform int enableVirtual;\n") + "uniform sampler2D inputImageTextureVirtual;\n") + " uniform sampler2D inputImageTexture;\n uniform sampler2D inputImageTexture2;\n uniform lowp float alphaValue;\n uniform lowp float softenLevel;\n \n void main()\n {\n     mediump vec4 yuv = texture2D(inputImageTexture, textureCoordinate);\n     \n mediump float sum = 0.0; \n  \n     \n mediump float sumr = 0.0; \n  \n     \n mediump float sumg = 0.0; \n  \n     \n mediump float sumb = 0.0; \n  \n") + String.format(Locale.US, " sum += texture2D(inputImageTexture, blurCoordinates[0]).a * %f; \n", new Object[]{Float.valueOf(fArr[0])})) + String.format(Locale.US, " sumr += texture2D(inputImageTexture, blurCoordinates[0]).r * %f; \n", new Object[]{Float.valueOf(fArr[0])})) + String.format(Locale.US, " sumg += texture2D(inputImageTexture, blurCoordinates[0]).g * %f; \n", new Object[]{Float.valueOf(fArr[0])})) + String.format(Locale.US, " sumb += texture2D(inputImageTexture, blurCoordinates[0]).b * %f; \n", new Object[]{Float.valueOf(fArr[0])});
        int i5 = 0;
        while (i5 < i2) {
            float f4 = fArr[(i5 * 2) + 1] + fArr[(i5 * 2) + 2];
            str = (((((((str +
                    String.format(Locale.US, " sum += texture2D(inputImageTexture, blurCoordinates[%d]).a * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 1), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sum += texture2D(inputImageTexture, blurCoordinates[%d]).a * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 2), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sumr += texture2D(inputImageTexture, blurCoordinates[%d]).r * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 1), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sumr += texture2D(inputImageTexture, blurCoordinates[%d]).r * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 2), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sumg += texture2D(inputImageTexture, blurCoordinates[%d]).g * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 1), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sumg += texture2D(inputImageTexture, blurCoordinates[%d]).g * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 2), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sumb += texture2D(inputImageTexture, blurCoordinates[%d]).b * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 1), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sumb += texture2D(inputImageTexture, blurCoordinates[%d]).b * %f; \n", new Object[]{Integer.valueOf((i5 * 2) + 2), Float.valueOf(f4)});
            i5++;
        }
        if (i4 > i2) {
            str = str + "highp vec2 singleStepOffset = vec2(texelWidthOffset, texelHeightOffset);\n";
            while (i2 < i4) {
                f2 = fArr[(i2 * 2) + 1];
                float f4 = fArr[(i2 * 2) + 2];
                f2 = ((f2 * ((float) ((i2 * 2) + 1))) + (f4 * ((float) ((i2 * 2) + 2)))) / (f2 + f4);
                str = (((((((str + String.format(Locale.US, "sum += texture2D(inputImageTexture, blurCoordinates[0] + singleStepOffset * %f).a * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sum += texture2D(inputImageTexture, blurCoordinates[0] - singleStepOffset * %f).a * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sumr += texture2D(inputImageTexture, blurCoordinates[0] + singleStepOffset * %f).r * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sumr += texture2D(inputImageTexture, blurCoordinates[0] - singleStepOffset * %f).r * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sumg += texture2D(inputImageTexture, blurCoordinates[0] + singleStepOffset * %f).g * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sumg += texture2D(inputImageTexture, blurCoordinates[0] - singleStepOffset * %f).g * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sumb += texture2D(inputImageTexture, blurCoordinates[0] + singleStepOffset * %f).b * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sumb += texture2D(inputImageTexture, blurCoordinates[0] - singleStepOffset * %f).b * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)});
                i2++;
            }
        }
        return ((((str + " mediump float delta = yuv.g - sum;\n mediump float alpha = 0.0;   \n if (delta < 0.0){ \n   alpha = 8.0 * pow((delta + 0.5), 4.0); \n } else {   \n   alpha = 1.0 - 8.0 * pow((0.5 - delta), 4.0); \n } \n  lowp float lightr = texture2D(inputImageTexture2, vec2(yuv.r, 0.5)).r;    \n lowp float lightg = texture2D(inputImageTexture2, vec2(yuv.g, 0.5)).g;    \n lowp float lightb = texture2D(inputImageTexture2, vec2(yuv.b, 0.5)).b;    \n lowp float rrr = mix(lightr, yuv.r, alpha);    \n lowp float ggg = mix(lightg, yuv.g, alpha);    \n lowp float bbb = mix(lightb, yuv.b, alpha);    \n    \n mediump vec4 tc = vec4(rrr, ggg, bbb, 1.0); tc = mix(yuv, tc, softenLevel);\n") + " if (enableBlur == 1) {\n\tlowp float xdistance = distance(textureCoordinate, vec2(0.5));\n\tif (xdistance > blurRadiusFromCenter) {\n\t    tc.rgb = mix(tc.rgb, vec3(sumr, sumg, sumb), clamp((xdistance-blurRadiusFromCenter)*10.0, 0.0, 0.7));\n\t  }\n\t}\n") + " mediump vec4 virtual_yuv = texture2D(inputImageTextureVirtual, textureCoordinate);\n") + " if (enableVirtual == 1) {\n\ttc = vec4(tc.rgb*virtual_yuv.rgb, 0.0);\n }\n") + " gl_FragColor = vec4(tc.rgb, 1.0); \n }";
    }

    public static String getFragmentShader2(int i, float f, boolean useOESImage) {
        String str;
        String str2;
        if (i < 1) {
            i = 1;
        }
        float[] fArr = new float[(i + 1)];
        float f2 = 0.0f;
        int i2 = 0;
        while (i2 < i + 1) {
            float f3;
            fArr[i2] = (float) ((1.0d / Math.sqrt((2.0d * Math.PI) * Math.pow((double) f, 2.0d))) * Math.exp((-Math.pow((double) i2, 2.0d)) / (2.0d * Math.pow((double) f, 2.0d))));
            if (i2 == 0) {
                f3 = fArr[i2] + f2;
            } else {
                f3 = (float) (((double) f2) + (2.0d * ((double) fArr[i2])));
            }
            i2++;
            f2 = f3;
        }
        for (int i3 = 0; i3 < i + 1; i3++) {
            fArr[i3] = fArr[i3] / f2;
        }
        int min = Math.min((i / 2) + (i % 2), getMaxVaryingVectors());
        int i4 = (i / 2) + (i % 2);
        if (useOESImage) {
            str = "#extension GL_OES_EGL_image_external : require \n";
            str2 = " uniform samplerExternalOES inputImageTexture;\n";
        } else {
            str = " \n";
            str2 = " uniform sampler2D inputImageTexture;\n";
        }
        str2 = (((str + "  varying highp vec2 textureCoordinate;\n" + " \n"
                + "  uniform highp float texelWidthOffset; \n"
                + "  uniform highp float texelHeightOffset;\n")
                + String.format(Locale.US, " varying highp vec2 blurCoordinates[%d];\n", new Object[]{Integer.valueOf((min * 2) + 1)}))
                + str2 + " uniform lowp float brightness;\n"
                + " void main()\n"
                + " {\n" + "     mediump vec4 yuv = texture2D(inputImageTexture, textureCoordinate);\n"
                + "     \n" + " mediump float sum = 0.0; \n" + "  \n")
                + String.format(Locale.US, " sum += texture2D(inputImageTexture, blurCoordinates[0]).g * %f; \n", new Object[]{Float.valueOf(fArr[0])});
        i2 = 0;
        while (i2 < min) {
            float f4 = fArr[(i2 * 2) + 1] + fArr[(i2 * 2) + 2];
            str2 = (str2 + String.format(Locale.US, " sum += texture2D(inputImageTexture, blurCoordinates[%d]).g * %f; \n", new Object[]{Integer.valueOf((i2 * 2) + 1), Float.valueOf(f4)})) +
                    String.format(Locale.US, " sum += texture2D(inputImageTexture, blurCoordinates[%d]).g * %f; \n", new Object[]{Integer.valueOf((i2 * 2) + 2), Float.valueOf(f4)});
            i2++;
        }
        if (i4 > min) {
            str2 = str2 + "highp vec2 singleStepOffset = vec2(texelWidthOffset, texelHeightOffset);\n";
            i2 = min;
            while (i2 < i4) {
                f2 = fArr[(i2 * 2) + 1];
                float f4 = fArr[(i2 * 2) + 2];
                f2 = ((f2 * ((float) ((i2 * 2) + 1))) + (f4 * ((float) ((i2 * 2) + 2)))) / (f2 + f4);
                str2 = (str2 + String.format(Locale.US, "sum += texture2D(inputImageTexture, blurCoordinates[0] + singleStepOffset * %f).g * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)})) +
                        String.format(Locale.US, "sum += texture2D(inputImageTexture, blurCoordinates[0] - singleStepOffset * %f).g * %f;\n", new Object[]{Float.valueOf(f2), Float.valueOf(r8)});
                i2++;
            }
        }
        return str2 + "  \n gl_FragColor = vec4(yuv.rgb + vec3(brightness),sum); \n }";
    }
}
